#include "ProjectInliersModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.project_inliers_model.json";
}

ProjectInliersModel::ProjectInliersModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/project_inliers.html");
}

ProjectInliersModel& ProjectInliersModel::get()
{
    static ProjectInliersModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void ProjectInliersModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);

    coefficientX_ = json["coefficientX"].toDouble(coefficientX_);
    coefficientY_ = json["coefficientY"].toDouble(coefficientY_);
    coefficientZ_ = json["coefficientZ"].toDouble(coefficientZ_);
    coefficientB_ = json["coefficientB"].toDouble(coefficientB_);
}

QJsonObject ProjectInliersModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));

    json.insert("coefficientX", QJsonValue(coefficientX_));
    json.insert("coefficientY", QJsonValue(coefficientY_));
    json.insert("coefficientZ", QJsonValue(coefficientZ_));
    json.insert("coefficientB", QJsonValue(coefficientB_));

    return json;
}
